package org.monkey.example.request.config;

import lombok.extern.slf4j.Slf4j;
import org.monkey.example.request.handler.RequestLogSaveHandler;
import org.monkey.request.plugin.service.RequestLogHandleContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Slf4j
@Configuration
@EnableScheduling
public class RequestConfigBean implements InitializingBean {

  @Autowired
  RequestLogHandleContext requestLogHandleContext;
  @Autowired
  RequestLogSaveHandler requestLogSaveHandler;

  @Override
  public void afterPropertiesSet(){
    requestLogHandleContext.addAfterHandlerLast(requestLogSaveHandler);
  }

  /**
   * 定期落库请求数据
   */
  @Scheduled(initialDelay = 2000, fixedDelay = 30000)
  public void requestSave() {
    requestLogSaveHandler.requestSave();
  }
}
